﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ana_Form
{
    public partial class Form_Depo : Form
    {
        DepodataDataContext db;
        Depo depo;

        public Depo Depos
        {
            get { return depo; }
            set { depo = value; }
        }

        public string Markas
        {
            get { return Depos.Adi; }
        }
        public Form_Depo()
        {
            InitializeComponent();
            db = new DepodataDataContext();
            markalarilistele();
        }
        public Form_Depo(ref DepodataDataContext DB)
        {
            InitializeComponent();
            db = DB;
            markalarilistele();
        }

        private void markalarilistele(List<Marka> soru)
        {
            listmalzeme.Items.Clear();
            foreach (var item in soru)
            {
                ListViewItem litem = new ListViewItem();
                litem.Text = item.id.ToString();
                litem.SubItems.Add(item.Adi);
                listmalzeme.Items.Add(litem);
            }
        }
        private void markalarilistele()
        {
            listmalzeme.Items.Clear();
            var soru = from x in db.Depos orderby x.Adi select x;
            foreach (var item in soru)
            {
                ListViewItem litem = new ListViewItem();
                litem.Text = item.id.ToString();
                litem.SubItems.Add(item.Adi);
                listmalzeme.Items.Add(litem);
            }

        }
        private void markalarilistele(IQueryable<Depo> soru)
        {
            listmalzeme.Items.Clear();
            foreach (var item in soru)
            {
                ListViewItem litem = new ListViewItem();
                litem.Text = item.id.ToString();
                litem.SubItems.Add(item.Adi);
                listmalzeme.Items.Add(litem);
            }

        }

        private void lblekle_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            btnduzenle.Enabled = true;
            btnduzenle.Tag = 1;
            btnduzenle.Text = "Ekle";
        }

        private void lblduzenle_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            btnduzenle.Text = "Değiştir";
            btnduzenle.Tag = 0;
            btnduzenle.Enabled = true;
            btnsil.Enabled = true;
        }

        private void listmalzeme_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                btnTamam.Enabled = true;
                btnduzenle.Enabled = true;
                btnsil.Enabled = true;

                Depos = (from x in db.Depos where x.id == int.Parse(listmalzeme.SelectedItems[0].Text) select x).FirstOrDefault();

            }
            catch (Exception)
            {
            }
        }


        private void btnekle_Click(object sender, EventArgs e)
        {
            Form_DepoGirdisi1 frm = new Form_DepoGirdisi1(ref db);
            if (frm.ShowDialog()==DialogResult.OK)
            {
                depo = new Depo();
                depo = frm.Depo;
            }
            markalarilistele();
            listmalzeme.SelectedItems[listmalzeme.Items[depo.id.ToString()].Index].Selected = true;
        }


        private void btniptal_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnsil_Click(object sender, EventArgs e)
        {
            if (Depos.DepoDetays.Count < 1)
            {
                if (MessageBox.Show("Bu Kayıt Database den Tamamen Kaldırılacaktır\nOnaylıyormusunuz?...", "Uyarı", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {

                    db.Depos.DeleteOnSubmit(Depos);
                    db.SubmitChanges();
                    markalarilistele();
                }
            }
            else
            {
                MessageBox.Show("Kayıt; Bağlantılı kayıtlar nedeniyle silinemez.");
            }
        }

        private void listmalzeme_Resize(object sender, EventArgs e)
        {
            listmalzeme.Columns[1].Width = listmalzeme.Width - 75;
        }

        private void btnTamam_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void btnduzenle_Click(object sender, EventArgs e)
        {
            string mark = InputBox.Input.Yaz("Depo İsmini Düzenleyiniz.", listmalzeme.SelectedItems[0].SubItems[1].Text);
            depo.Adi = mark;
            db.SubmitChanges();

            markalarilistele();
        }

        private void listmalzeme_DoubleClick(object sender, EventArgs e)
        {
            btnTamam_Click(null, null);

        }

        private void Form_Marka_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                btniptal_Click(null, null);
            }
        }

        private void txtarama_TextChanged(object sender, EventArgs e)
        {
            listmalzeme.Items.Clear();
            var soru = from x in db.Depos
                       where x.Adi.ToUpper().Contains(txtarama.Text.ToUpper())
                       orderby x.Adi
                       select x;
            markalarilistele(soru);
        }



    }
}
